What's the meaning of 'char (*p)[5];'?
Posted
by
jpmelos
on Stack Overflow
See other posts from Stack Overflow
or by jpmelos
Published on 2011-06-24T12:12:55Z
Indexed on
2011/06/24
16:22 UTC
Read the original article
Hit count: 148
people.
I'm trying to grasp the differences between these three declarations:
char p[5];
char *p[5];
char (*p)[5];
I'm trying to find this out by doing some tests, because every guide of reading declarations and stuff like that has not helped me so far. I wrote this little program and it's not working (I've tried other kinds of use of the third declaration and I've ran out of options):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char p1[5];
char *p2[5];
char (*p3)[5];
strcpy(p1, "dead");
p2[0] = (char *) malloc(5 * sizeof(char));
strcpy(p2[0], "beef");
p3[0] = (char *) malloc(5 * sizeof(char));
strcpy(p3[0], "char");
printf("p1 = %s\np2[0] = %s\np3[0] = %s\n", p1, p2[0], p3[0]);
return 0;
}
The first and second works alright, and I've understood what they do. What is the meaning of the third declaration and the correct way to use it?
Thank you!
© Stack Overflow or respective owner